package com.itheima.springbootfastjson2.controller;

import com.itheima.springbootfastjson2.domain.Result;
import com.itheima.springbootfastjson2.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/hello")
    public Result hello() {
        User user = new User();
        user.setId(1);
        user.setName("张三");
        return Result.success(user);
    }

    @RequestMapping("/saveRedis")
    public Result saveRedis() {
        User user = new User();
        user.setId(1);
        user.setName("张三");

        ValueOperations<String, User> valueOperations = redisTemplate.opsForValue();
        valueOperations.set("user", user);

        return Result.success();
    }

    @RequestMapping("/getRedis")
    public Result<User> getRedis() {
        User user = (User) redisTemplate.opsForValue().get("user");
        return Result.success(user);
    }
}
